package com.sakura.mall.order.orderReturnReason.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.orderReturnReason.domain.entity.OrderReturnReasonEntity;
import com.sakura.mall.order.orderReturnReason.domain.request.AddOrderReturnReasonRequest;
import com.sakura.mall.order.orderReturnReason.domain.request.UpdateOrderReturnReasonRequest;
import com.sakura.mall.order.orderReturnReason.domain.request.OrderReturnReasonPageRequest;
import com.sakura.mall.order.orderReturnReason.domain.vo.OrderReturnReasonVo;
import com.sakura.mall.order.orderReturnReason.mapper.OrderReturnReasonMapper;
import com.sakura.mall.order.orderReturnReason.service.OrderReturnReasonService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 退货原因 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class OrderReturnReasonServiceImpl extends ServiceImpl<OrderReturnReasonMapper, OrderReturnReasonEntity> implements OrderReturnReasonService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddOrderReturnReasonRequest request) {
        // todo 其他校验逻辑
        OrderReturnReasonEntity orderReturnReason = request.convert(OrderReturnReasonEntity.class);
        this.save(orderReturnReason);
    }

    @Override
    public Page<OrderReturnReasonVo> queryPage(OrderReturnReasonPageRequest request) {
        IPage<OrderReturnReasonEntity> qp = request.toPage();
        LambdaQueryWrapper<OrderReturnReasonEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<OrderReturnReasonVo> ret = qp.convert(entity -> entity.convert(OrderReturnReasonVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateOrderReturnReasonRequest request) {
        OrderReturnReasonEntity entity = request.convert(OrderReturnReasonEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

